<template>
<div class="login-box">
  <!-- 导航栏 -->
  <van-nav-bar class="page-nav-bar" title="登录">
    <template #left>
      <van-icon @click="$router.go(-1)" color="#fff" name="cross" size="18" />
    </template>
  </van-nav-bar>
  <!-- 表单 -->
  <van-form @submit="onSubmit" ref="loginForm">
  <!-- type="numder",html5中方法 ,vant也是封装input标签-->
  <!-- native-type="button",不会验证 -->
  <van-field
  type="number"
  maxlength="11"
  v-model="user.mobile"
  name="mobile"
  placeholder="请输入手机号"
  :rules="userFormRules.mobile">
    <i slot="left-icon" class="iconfont icon-shouji"></i>
  </van-field>

  <van-field
  type="number"
  maxlength="6"
  v-model="user.code"
  name="code"
  placeholder="请输入验证码"
  :rules="userFormRules.code">
    <i slot="left-icon"  class="iconfont icon-yanzhengma"></i>
  <template #button>
    <van-count-down
    @finish="isCountShow = false"
    v-if="isCountShow"
    :time="60000"
    format=" ss s" />
    <van-button v-else round
    size="small"
    class="yanzhengma"
    type="default"
    @click="onSendSms"
    native-type="button">
      发送验证码
    </van-button>
  </template>
  </van-field>

  <div class="login-btn-wrap">
    <van-button class="login-btn"
    block type="info"
    native-type="submit">
      登录
    </van-button>
  </div>
</van-form>

</div>
</template>

<script>
import { login, sendSms } from '@/api/user.js'
export default {
  name: 'login',
  data () {
    return {
      user: {
        mobile: '',
        code: ''
      },
      // 验证规则
      userFormRules: {
        mobile: [
          {
            required: true,
            message: '手机号不能为空'
          },
          {
            pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: '手机号码填写错误'
          }
        ],
        code: [
          {
            required: true,
            message: '验证码不能为空'
          },
          {
            pattern: /^\d{6}$/,
            message: '验证码填写错误'
          }
        ]

      },
      // 控制倒计时显示
      isCountShow: false
    }
  },

  created () {

  },

  methods: {
    // 点击提交
    async onSubmit () {
      // 获取到输入框的值
      // 数据校验
      // 发送请求
      const user = this.user
      this.$toast.loading({
      // 持续展示
        duration: 0,
        message: '登录中',
        forbidClick: true
      })
      try {
        const { data: res } = await login(user)
        // console.log(res)
        this.$store.commit('changeUser', res.data)
        this.$toast.success('登录成功', res)
        this.$router.go(-1)
      } catch (err) {
        if (err.response.status === 400) {
          this.$toast.fail('手机或者验证码错误', err)
        } else {
          this.$toast.fail('未知错误，请稍后')
        }
      }
    },

    // 点击发送验证码
    async onSendSms () {
      // 校验手机号
      try {
        await this.$refs.loginForm.validate('mobile')
      } catch {
        return false
      }
      // console.log('验证通过')
      // 验证通过，显示倒计时
      this.isCountShow = true
      // 请求发送验证码
      try {
        await sendSms(this.user.mobile)
        this.$toast.success('发送成功')
      } catch (err) {
        // this.$toast.fail('获取失败')
        this.isCountShow = false
        if (err.response.status === 429) {
          return this.$toast.fail('亲，发送太频繁')
        }
        this.$toast.fail('亲，发送失败')
      }
    }
  }
}
</script>

<style scoped lang='less'>
.login-box .iconfont {
  font-size: 37px;
}
.yanzhengma {
  background-color: #ededed;
  font-size:22px;
  width: 160px;
  height: 46px;
  line-height: 46px;

}
.login-btn-wrap {
  margin: 53px 28px;
  .login-btn {
     background-color: #3296fa;
     border: none;
  }
}
</style>
